﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes
{
    using System.Web.UI.WebControls;
    public class PrivacyDropDownList : DropDownList
    {
        bool _hideNoneOption = false;
        bool _itemAdded = false;

        public bool HideNobodyOption { get { return _hideNoneOption; } set { _hideNoneOption = value; } }

        public PrivacyDropDownList()
            : base()
        {
            
            this.PreRender += new EventHandler(PrivacyDropDownList_PreRender);
            this.DataBound += new EventHandler(PrivacyDropDownList_DataBound);
            this.DataBinding += new EventHandler(PrivacyDropDownList_DataBinding);
        }

        void PrivacyDropDownList_DataBinding(object sender, EventArgs e)
        {
            AddItems();
        }

        void PrivacyDropDownList_DataBound(object sender, EventArgs e)
        {
            
        }

        void PrivacyDropDownList_PreRender(object sender, EventArgs e)
        {

            AddItems();
            try
            {
                this.Items.FindByValue(((int)_privacy).ToString()).Selected = true;
            }
            catch { }
        }

        void AddItems()
        {
            if (!_itemAdded)
            {
                _itemAdded = true;
                Items.Add(new ListItem("所有人可见", "1"));
                Items.Add(new ListItem("仅好友可见", "2"));
                if (!_hideNoneOption)
                {
                    Items.Add(new ListItem("隐藏", "3"));
                }
            }
        }

        private PrivacyEnum _privacy;

        public PrivacyEnum NamedPrivacy
        {
            get 
            {
                try
                {
                    return (PrivacyEnum)int.Parse(SelectedValue);
                }
                catch { return PrivacyEnum.None; }
            }
            set
            {
                _privacy = value;
            }
        }


        public string Privacy
        {
            get
            {
                return this.SelectedValue;
            }
            set
            {
                try
                {
                    this.Items.FindByValue(value).Selected = true;
                }
                catch { }
            }
        }
    }
}
